import {Injectable} from '@angular/core';
import {Hero} from "../models/hero";
import {BackendService} from "./backend.service";
import {Logger} from "./logger.service";
import {Observable} from "rxjs";

@Injectable()
export class HeroesService {
  constructor(private backend: BackendService,
              private logger: Logger) {
  }

  getHeroes(): Observable<Hero[]> {
    return Observable.fromPromise(this.backend.getAll(Hero)).do((heroes)=> {
      this.logger.log(`Fetched ${heroes.length} heroes.`)
    })
  }

  getHero(id): Observable<Hero> {
    return Observable.fromPromise(this.backend.getAll(Hero)).map(heroes=> {
      return heroes.find(hero => hero.id === id)
    })
  }
}
